package cc.sheji.config;

import cc.sheji.domain.ErrorCode;
import cc.sheji.domain.JsonResult;
import cc.sheji.exception.*;
import cn.dev33.satoken.exception.NotLoginException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public JsonResult defaultException(Exception e, HttpServletRequest request, HttpServletResponse response) {
        e.printStackTrace();
        return new JsonResult(ErrorCode.UNKOWN, "未知错误");
    }

    @ResponseBody
    @ExceptionHandler(DataIntegrityViolationException.class)
    public JsonResult defaultException(DataIntegrityViolationException e, HttpServletRequest request, HttpServletResponse response) {
        e.printStackTrace();
        return new JsonResult(ErrorCode.UNKOWN, "存在重复数据");
    }

    @ResponseBody
    @ExceptionHandler(TooMoreActionException.class)
    public JsonResult defaultException(TooMoreActionException e, HttpServletRequest request, HttpServletResponse response) {
        e.printStackTrace();
        return new JsonResult(ErrorCode.UNKOWN, "频繁提交任务，一分钟后重试");
    }

    @ResponseBody
    @ExceptionHandler(NotUserException.class)
    public JsonResult defaultException(NotUserException e, HttpServletRequest request, HttpServletResponse response) {
        return new JsonResult(ErrorCode.NotUserException, "用户不存在");
    }

    @ResponseBody
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public JsonResult defaultException(HttpRequestMethodNotSupportedException e, HttpServletRequest request, HttpServletResponse response) {
        return new JsonResult(ErrorCode.NOT_FUCTION, "访问路径问题");
    }

    @ResponseBody
    @ExceptionHandler(NotEnoughCoinException.class)
    public JsonResult defaultException(NotEnoughCoinException e, HttpServletRequest request, HttpServletResponse response) {

        return new JsonResult(ErrorCode.NotEnoughCoin, e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(CreateJobRuleException.class)
    public JsonResult defaultException(CreateJobRuleException e, HttpServletRequest request, HttpServletResponse response) {

        return new JsonResult(ErrorCode.CreateJobRuleException, e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(NotLoginException.class)
    public JsonResult defaultException(NotLoginException e, HttpServletRequest request, HttpServletResponse response) {
        return new JsonResult(ErrorCode.NOT_PERMISSION, "没有登录");
    }

    @ResponseBody
    @ExceptionHandler(ConstraintViolationException.class)
    public JsonResult defaultException(ConstraintViolationException e, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("defaultException handle:" + e);
        Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
        ConstraintViolation<?> constraintViolation = violations.stream().findFirst().get();
        String message = constraintViolation.getMessage();
        return new JsonResult(ErrorCode.ConstraintViolationException, message);
    }

    @ResponseBody
    @ExceptionHandler(org.springframework.validation.BindException.class)
    public JsonResult defaultException(org.springframework.validation.BindException e, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("defaultException handle1:");
        return new JsonResult(ErrorCode.ConstraintViolationException, e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }

    @ResponseBody
    @ExceptionHandler(PasswordException.class)
    public JsonResult defaultException(PasswordException e, HttpServletRequest request, HttpServletResponse response) {
        return new JsonResult(ErrorCode.PasswordException, "用户名错误或者密码错误");
    }

}
